package com.roadmap.struts.action.admin.life;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.roadmap.biz.common.IManager;
import com.roadmap.biz.common.ManagerFactory;
import com.roadmap.common.BaseEntity;
import com.roadmap.common.RdcConst;
import com.roadmap.exception.EntityNotFoundException;
import com.roadmap.struts.action.ActionConst;
import com.roadmap.struts.action.RdcAction;

public class CommentAction extends RdcAction {

	public CommentAction() {
		super("getcomments");
	}

	@Override
	public ActionForward performAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String ownerId = request
				.getParameter(ActionConst.Aspirant_Id_In_Request);
		if (ownerId == null || ownerId.trim().length() == 0) {
			return handleInvalidUser(request, "/comment.do");
		}

		String curUserId = getUIDFromSession(request);
		String pid = request.getParameter(ActionConst.Parent_Id_In_Request);
		String type = "Execution";
		/*
		 * String type = request.getParameter("type");
		 * 
		 * if (RdcConst.getEntityNameClazz(type) == null) { throw new
		 * InvalidValueException(String.format( "%s is not specified type",
		 * type)); }
		 */
		IManager parentMgr = ManagerFactory.getInstance(type, curUserId,
				ownerId);
		BaseEntity parent = parentMgr.get(pid);
		if (parent == null) {
			throw new EntityNotFoundException(String.format(
					"Parent entity %s was not found.", pid));
		}

		request.setAttribute("parentEntry", parent);

		int currentPage = getCurrentPageNum(request);
		IManager cmMgr = ManagerFactory.getInstance(RdcConst.COMMENT,
				curUserId, ownerId, type);
		cmMgr.setPage(currentPage);

		List comments = cmMgr.getEntitiesOfParent(pid);
		if (comments != null && !comments.isEmpty()) {
			request.setAttribute("comments", comments);
			createPagingInfoInRequest(request, cmMgr);
		}
		return mapping.findForward("getcomments");
	}

	@Override
	public boolean isFormSubmitted(HttpServletRequest request) throws Exception {
		return false;
	}

}
